/**
 * Created by Administrator on 2016/8/23.
 */

var router = require("express").Router();
var taskDao = require('./taskModel');


function getTaskList(req,res){
    var flag = 0;
    if(req.body != null){
        var taskType = req.body.taskType;
        var page = req.body.page;
        var size = req.body.size;
    }else{
        return res.json(200,'error');
    }

    var object = {
        results:[],
        counts:0
    }

    taskDao.getTaskList(taskType,page,size,function(err,results){
        if(err){

            console.log(err);
            return res.json(500,err);
        }
        taskDao.getTaskCount(taskType,function(err,counts){
            if(err){

                console.log(err);
                return res.json(500,err);
            }
            if(counts == 0){
                return res.json(200,object);
            }
            object.counts = counts;
            results.forEach(function(item){
                taskDao.getChildTask(item.task_id,function(err,childTask){
                    if(err){
                        console.log(err);
                        return res.json(500,err);
                    }
                    item.childTask = childTask;
                    flag++;
                    if(flag == results.length){
                        object.results = results;
                        return res.json(200,object);
                    }
                })
            })

        })


    });
}


function changeTaskStatus(req,res){
    if(req.query != null&&req.query != ''){
        var taskId = req.query.taskId;
        var taskStatus = req.query.taskStatus;
    }else{
        return res.json(200,'error');
    }
    taskDao.updateTaskStatus(taskId,taskStatus,function(err,results){
        if(err){

            console.log(err);
            return res.json(500,err);
        }

        return res.json(200,true);
    });
}


function changeTaskState(req,res){
    if(req.query != null&&req.query != ''){
        var taskId = req.query.taskId;

    }else{
        return res.json(200,'error');
    }
    taskDao.changeTaskState(taskId,function(err,results){
        if(err){

            console.log(err);
            return res.json(500,err);
        }

        return res.json(200,true);
    });
}


function getTaskDetail(req,res){
    if(req.query != null&&req.query != ''){
        var taskId = req.query.taskId;

    }else{
        return res.json(200,'error');
    }
    taskDao.getTaskByTaskId(taskId,function(err,data){
        if(err){
            console.log(err);
            return res.json(500,err);
        }
        if(data.length > 0){
            var results = data[0];
        }else{
            var results = null;
        }
        taskDao.getChildTask(taskId,function(err,childTask){
            if(err){
                console.log(err);
                return res.json(500,err);
            }
            if(results){
                results.childTask = childTask;
            }

            console.log(results);
            return res.json(200,results);
        })



    });
}

//创建任务
function createTask(req,res){
    var flag = 0;
    if(req.body != null&&req.body != ''){
        var form = req.body;
    }else{
        return res.json(200,'error');
    }
    //对传入的参数进行一些处理
    for(i=0;i<form.taskItems.length;i++){
        form.taskItems[i].ranking = i+1;
        if(i>0){
            if(!form.taskItems[i].taskItemLink){
                form.taskItems[i].taskItemLink = form.taskItems[0].taskItemLink;
            }
            if(!form.taskItems[i].packetName){
                form.taskItems[i].packetName = form.taskItems[0].packetName;
            }
        }
    }

    //插入总任务
    var taskName = form.taskName;
    var icon = form.icon;
    var taskMoney = form.taskMoney;
    var taskQuantity = form.taskQuantity;
    var taskItemsNumber = form.taskItemsNumber;
    var taskType = form.taskType;
    taskDao.insertTask(taskName,icon,taskMoney,taskQuantity,taskItemsNumber,taskType,function(err,data){
        if(err){
            return res.json(500,err);
        }
        var taskId = data.insertId;
        form.taskItems.forEach(function(item){
            var taskItemName = item.taskItemName;
            var taskItemType = item.taskItemType;
            var taskItemMoney = item.taskItemMoney;
            var describe = item.describe;
            var taskItemLink = item.taskItemLink;
            var openTimeInterval = item.openTimeInterval;
            var ranking = item.ranking;
            if(taskItemType == 2){   //分享任务
                var shareTitle = item.packetName;
                var packetName = '';
            }else{
                var shareTitle = '';
                var packetName = item.packetName;
            }

            taskDao.insertTaskItem(taskId,taskItemName,taskItemType,taskItemMoney,describe,taskItemLink,openTimeInterval,ranking,packetName,shareTitle,function(err,data){
                if(err){
                    return res.json(500,err);
                }
                flag++;
                if(flag == form.taskItems.length){
                    return res.json(200,true);
                }
            })


        })

    })



}

//更新任务
function updateTask (req,res){
    console.log(req.body);
    if(req.body != null&&req.body != ''){
        var form = req.body;
    }else{
        return res.json(200,'error');
    }

    var taskId = form.task_id;
    var taskName = form.task_name;
    var taskIcon = form.task_icon;
    //var taskMoney = form.task_money
    var taskTotalQuantity = form.task_total_quantity;
    var taskLeftQuantity = form.task_left_quantity;
    taskDao.updateTask(taskId,taskName,taskIcon,taskTotalQuantity,taskLeftQuantity,function(err,data){
        if(err){
            return res.json(500,err);
        }
        return res.json(200,true);
    })

}


router.post("/getTaskList", getTaskList);
router.get("/changeTaskStatus", changeTaskStatus);
router.get("/changeTaskState", changeTaskState);
router.get("/getTaskDetail", getTaskDetail);
router.post("/createTask", createTask);
router.post("/updateTask", updateTask);



module.exports = router;